package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.SheetDialog;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.SingleAlertDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.ExamEditAct;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by ws.
 * on Data:2022/2/16  Time:下午 3:54.
 * description: 考试批阅.
 */
public class ExamReviewFrag extends BaseMvpFragment {
    private boolean isEdit;
    private int type;

    public static ExamReviewFrag getInstance(int type) {
        ExamReviewFrag frag = new ExamReviewFrag();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type", 0);
        isEdit = type == 1;
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return new BaseMvpPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_exam_review;
    }

    @Override
    public void initView() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ReviewAdapter mAdapter = new ReviewAdapter(testData());
        recyclerView.setAdapter(mAdapter);
        findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("打回",0,0))
                        .addSheet(new SheetDialog.Sheet("催交",0,1))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                ExamEditAct.start(sheet.action);
                            }
                        }).show();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {


    }

    private List<Remove> testData() {
        List<Remove> datas = new ArrayList<>();//模拟数据
        datas.add(new Remove(3));
        datas.add(new Remove(4));
        datas.add(new Remove(2));
        datas.add(new Remove(1));
        datas.add(new Remove(0));
        datas.add(new Remove(0));
        datas.add(new Remove(0));
        return datas;
    }

    class ReviewAdapter extends BaseQuickAdapter<Remove, BaseViewHolder> {
        public ReviewAdapter(@Nullable List<Remove> data) {
            super(R.layout.item_exam_review, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Remove item) {
            ImageView ic_avatar = helper.getView(R.id.ic_avatar);
            GlideUtils.loadCircleImage(item.avatar, ic_avatar);
            helper.setText(R.id.tv_name, item.name);
            ViewGroup ll_right = helper.getView(R.id.ll_right);
            ll_right.setVisibility(View.GONE);
            TextView rTxt = ((TextView) ll_right.getChildAt(0));


            ViewGroup ll_sub_title = helper.getView(R.id.ll_sub_title);
            ll_sub_title.setVisibility(View.GONE);
            ll_sub_title.getChildAt(0).setVisibility(View.GONE);
            TextView subTxt = (TextView) ll_sub_title.getChildAt(1);

            SuperTextView stv_submit = helper.getView(R.id.stv_submit);
            stv_submit.setVisibility(View.GONE);
            stv_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new SingleAlertDialog(getContext())
                            .setMsg(new SpanUtils().append("催交过于频繁，两次催交间隔必须大于一分钟").setForegroundColor(0xff67788A).create())
                            .show();
                }
            });
            switch (item.type) {
                case 0://未交
                case 4://打回
                    if (isEdit) {
                        stv_submit.setVisibility(View.VISIBLE);
                        stv_submit.setText("催交");
                        stv_submit.setTextColor(Color.WHITE);
                        stv_submit.setShaderEnable(true);
                        stv_submit.setStrokeWidth(0);
                    } else {
                        ll_right.setVisibility(View.VISIBLE);
                        ll_right.getChildAt(1).setVisibility(View.GONE);
                        rTxt.setText("未交");
                        rTxt.setTextColor(0xff4A9FFA);
                    }
                    if (item.type == 4) {
                        ll_sub_title.setVisibility(View.VISIBLE);
                        ll_sub_title.getChildAt(0).setVisibility(View.VISIBLE);
                        subTxt.setText(item.time);
                    }
                    break;
                case 1://已催交
                    if (isEdit) {
                        stv_submit.setVisibility(View.VISIBLE);
                        stv_submit.setText("再次催交");
                        stv_submit.setShaderEnable(false);
                        stv_submit.setTextColor(0xff4A9FFA);
                        stv_submit.setStrokeWidth(SizeUtils.dp2px(1));
                        ll_sub_title.getChildAt(0).setVisibility(View.GONE);
                        subTxt.setText("已催交1次");
                    } else {
                        ll_right.setVisibility(View.VISIBLE);
                        ll_right.getChildAt(1).setVisibility(View.GONE);
                        rTxt.setText("未交");
                        rTxt.setTextColor(0xff4A9FFA);
                    }
                    break;
                case 2://未批
                    ll_right.setVisibility(View.VISIBLE);
                    ll_right.getChildAt(1).setVisibility(View.VISIBLE);
                    rTxt.setText("未批");
                    rTxt.setTextColor(0xffF9485D);
                    break;
                case 3://已批
                    ll_sub_title.setVisibility(View.VISIBLE);
                    subTxt.setText(String.format("提交时间:%s 用时:%s", item.time, item.useTime));
                    ll_right.setVisibility(View.VISIBLE);
                    ll_right.getChildAt(1).setVisibility(View.VISIBLE);
                    SpanUtils.with(rTxt)
                            .append(item.score)
                            .setForegroundColor(0xff4A9FFA)
                            .setFontSize(20, true)
                            .append("分")
                            .setFontSize(12, true)
                            .setForegroundColor(0xffB4C1CE)
                            .create();

                    break;


            }
        }
    }

    public class Remove {
        String avatar = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202011%2F20%2F20201120091707_fkBTd.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647593115&t=329837342300c79dc42ebd0ae9fa83f7";
        int type;
        String name = "ALAN";
        String time = "21-3-21 12:23";
        String useTime = "00:00:10";
        String score = "18";

        public Remove(int type) {
            this.type = type;
        }
    }
}
